// ----------折线图-----------
function lineChart(a) {
  // console.log(a)
  let myChart = echarts.init(document.querySelector('#line'))
  let option = {
    title: {
      text: '2022全学科薪资走势',
      top: 15,
      left: 10,
      textStyle: {
        fontSize: 16
      }
    },
    xAxis: {
      type: 'category',
      data: a.map(item => item.month),
      // data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
      axisLable: {
        color: '#999'
      },
      axisLine: {
        lineStyle: {
          color: '#ccc',
          type: 'dashed'
        }
      }
    },
    yAxis: {
      type: 'value',
      splitLine: {
        lineStyle: {
          type: 'dashed'
        }
      }
    },
    series: [
      {
        data: a.map(item => item.salary),
        // data: [9000, 12000, 15000, 13000, 10000, 18000, 14000, 10000, 12000, 13000, 15000, 19000],
        type: 'line',
        smooth: true,
        lineStyle: {
          width: 6
        },
        symbolSize: 10,
        areaStyle: {
          color: {
            type: 'linear',
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: [{
              offset: 0, color: '#499FEE' // 0% 处的颜色
            }, {
              offset: 0.8, color: 'rgba(255,255,255,0.2)' // 80% 处的颜色
            }, {
              offset: 1, color: 'rgba(255,255,255,0)' // 100% 处的颜色
            }],
            global: false // 缺省为 false
          }
        }
      }
    ],
    grid: {
      top: '20%'
    },
    tooltip: {
      trigger: 'axis'
    },
    color: [{
      type: 'linear',
      x: 0,
      y: 0,
      x2: 0,
      y2: 1,
      colorStops: [{
        offset: 0, color: '#499FEE' // 0% 处的颜色
      }, {
        offset: 1, color: '#5D75F0' // 100% 处的颜色
      }],
      global: false // 缺省为 false
    }],
  }
  myChart.setOption(option)

}
// lineChart()

// ----------饼图(右上角)------------
function classSalaryChart(a) {
  // console.log(a)
  let myChart = echarts.init(document.querySelector('#salary'))
  let option = {
    title: {
      text: '班级薪资分布',
      top: 15,
      left: 10,
      textStyle: {
        fontSize: 16
      }
    },
    tooltip: {
      trigger: 'item'
    },
    legend: {
      bottom: '6%',
      left: 'center',
    },
    series: [
      {
        name: '班级薪资分布',
        type: 'pie',
        radius: ['50%', '64%'],

        itemStyle: {
          borderRadius: 10,
          borderColor: '#fff',
          borderWidth: 2
        },
        label: {
          show: false,
          position: 'center'
        },
        labelLine: {
          show: false
        },
        data: a.map(item => {
          return { value: item.g_count + item.b_count, name: item.label }
        })
        // data: [
        //   { value: 1048, name: '1万以下' },
        //   { value: 235, name: '1万-2万' },
        //   { value: 580, name: '1.5万-2万' },
        //   { value: 484, name: '2万以上' }
        // ]
      }
    ],
    color: ['#FDA224', '#5097FF', '#3ABCFA', '#34D39A']
  }
  myChart.setOption(option)
}
// classSalaryChart()
// ----------柱形图------------
function linesChart(a) {
  console.log(a)
  let myChart = echarts.init(document.querySelector('#lines'))
  option = {
    xAxis: {
      type: 'category',
      // data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
      data: a[1].map(item => item.name),
      axisLabel: {
        color: '#ccc'
      },
      axisLine: {
        lineStyle: {
          color: '#ccc',
          type: 'dashed'
        }
      }
    },
    yAxis: {
      type: 'value',
      splitLine: {
        lineStyle: {
          type: 'dashed'
        }
      }
    },
    tooltip: {},
    grid: {
      left: 70,
      top: 30,
      right: 30,
      bottom: 50
    },
    series: [
      {
        data: a[1].map(item => item.hope_salary),
        type: 'bar',
        name: '期望薪资' // 这个数据的名字，可以在鼠标移入的提示上显示
      },
      {
        data: a[1].map(item => item.salary),
        type: 'bar',
        name: '就业薪资' // 这个数据的名字，可以在鼠标移入的提示上显示
      }
    ],
    color: [{
      type: 'linear',
      x: 0,
      y: 0,
      x2: 0,
      y2: 1,
      colorStops: [{
        offset: 0, color: '#34D39A' // 0% 处的颜色
      }, {
        offset: 1, color: 'rgba(52,211,154,0.2)' // 100% 处的颜色
      }],
      global: false // 缺省为 false
    },
    {
      type: 'linear',
      x: 0,
      y: 0,
      x2: 0,
      y2: 1,
      colorStops: [{
        offset: 0, color: '#499FEE' // 0% 处的颜色
      }, {
        offset: 1, color: 'rgba(73,159,238,0.2)' // 100% 处的颜色
      }],
      global: false // 缺省为 false
    }],
  }
  myChart.setOption(option)
  document.querySelector('#btns').addEventListener('click', function (e) {
    if (e.target.tagName = 'BUTTON') {
      document.querySelector('#btns .btn-blue').classList.remove('btn-blue')
      e.target.classList.add('btn-blue')

      // 组号
      let i = e.target.innerHTML
      // 换图表中的数据
      option.xAxis.data = a[i].map(item => item.name)
      option.series[0].data = a[i].map(item => item.hope_salary)
      option.series[1].data = a[i].map(item => item.salary)
      myChart.setOption(option)
    }
  })
}
// linesChart()
// ----------饼图(左上角)-------------
function sexSalaryChart(a) {
  let myChart = echarts.init(document.querySelector('#gender'))
  let option = {
    title: [
      {
        text: '男女生薪资分布',
        top: 15,
        left: 10,
        textStyle: {
          fontSize: 16
        }
      },
      {
        text: '男生',
        top: '50%',
        left: '45%',
        textStyle: {
          fontSize: 12
        }
      }, {
        text: '女生',
        top: '90%',
        left: '45%',
        textStyle: {
          fontSize: 12
        }
      }],
    tooltip: {
      trigger: 'item'
    },

    series: [
      {
        name: '男生',
        type: 'pie',
        radius: ['20%', '30%'],
        center: ['50%', '30%'],
        data: a.map(item => {
          return { value: item.b_count, name: item.label }
        })
      },
      {
        name: '女生',
        type: 'pie',
        radius: ['20%', '30%'],
        center: ['50%', '70%'],
        data: a.map(item => {
          return { value: item.g_count, name: item.label }
        })
      }
    ],
    color: ['#FDA224', '#5097FF', '#3ABCFA', '#34D39A']
  }
  myChart.setOption(option)
}
// sexSalaryChart()
// ----------地图-------------
function mapChart(a) {
  const mapData = [
    { name: '南海诸岛', value: 0 },
    { name: '北京', value: 0 },
    { name: '天津', value: 0 },
    { name: '上海', value: 0 },
    { name: '重庆', value: 0 },
    { name: '河北', value: 0 },
    { name: '河南', value: 0 },
    { name: '云南', value: 0 },
    { name: '辽宁', value: 0 },
    { name: '黑龙江', value: 0 },
    { name: '湖南', value: 0 },
    { name: '安徽', value: 0 },
    { name: '山东', value: 0 },
    { name: '新疆', value: 0 },
    { name: '江苏', value: 0 },
    { name: '浙江', value: 0 },
    { name: '江西', value: 0 },
    { name: '湖北', value: 0 },
    { name: '广西', value: 0 },
    { name: '甘肃', value: 0 },
    { name: '山西', value: 0 },
    { name: '内蒙古', value: 0 },
    { name: '陕西', value: 0 },
    { name: '吉林', value: 0 },
    { name: '福建', value: 0 },
    { name: '贵州', value: 0 },
    { name: '广东', value: 0 },
    { name: '青海', value: 0 },
    { name: '西藏', value: 0 },
    { name: '四川', value: 0 },
    { name: '宁夏', value: 0 },
    { name: '海南', value: 0 },
    { name: '台湾', value: 0 },
    { name: '香港', value: 0 },
    { name: '澳门', value: 0 }
  ]

  mapData.forEach(item => {
    let 结果 = a.find(v => {
      return v.name.includes(item.name)
    })
    if (结果) {
      item.value = 结果.value
    }
  })
  let myChart = echarts.init(document.querySelector('#map'))
  let option = {
    title: {
      text: '籍贯分布',
      top: 10,
      left: 10,
      textStyle: {
        fontSize: 16
      }
    },
    series: [
      {
        name: '籍贯分布',
        type: 'map',
        map: 'china',
        data: mapData,
        label: {
          show: true,
          fontSize: 10,
          color: 'rgba(0,0,0,0.7)'
        },
        itemStyle: {
          areaColor: '#E0FFFF',
          borderColor: 'rgba(0,0,0,0.2)'
        },
        emphasis: {
          itemStyle: {
            areaColor: '#34d39a',
            shadowColor: 'rgba(0,0,0,0.5)',
            shadowOffsetX: 0,
            shadowOffsetY: 0,
            shadowBlur: 20,
            borderWidth: 0
          }
        },
      }
    ],
    tooltip: {
      formatter: '{b}:{c} 位学员',
      backgroundColor: 'rgba(0,0,0,0.5)',
      borderWidth: 0,
      textStyle: {
        color: '#fff'
      }
    },
    visualMap: {
      type: 'continuous',
      max: 7,
      min: 0,
      text: [7, 0],
      left: 15,
      bottom: 10,
      inRange: { color: ['#fff', '#0075f0'] }
    }
  }
  myChart.setOption(option)
}
// mapChart()



axios({
  url: '/dashboard',
}).then(result => {
  console.log(result)
  let { groupData, overview, provinceData, salaryData, year } = result.data.data

  for (let key in overview) {
    document.querySelector(`[name=${key}]`).innerHTML = overview[key]
  }
  // 传折线图数据
  lineChart(year)
  // 传饼图的数据
  classSalaryChart(salaryData)
  sexSalaryChart(salaryData)
  // 传柱状图的数据
  linesChart(groupData)
  // 传地图的数据
  mapChart(provinceData)
})
document.querySelector('#logout').addEventListener('click', function () {

  localStorage.removeItem('token')

  setTimeout(() => {
    location.href = './login.html'
  }, 1500)
})